/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import entities.Pyme;
import java.util.Date;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Andrés Felipe
 */
@Stateless
@Remote(PymeFacadeRemote.class)
public class PymeFacade extends AbstractFacade<Pyme> implements PymeFacadeRemote {

    @PersistenceContext(unitName = "Javeriana.EJB.EstimatorPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PymeFacade() {
        super(Pyme.class);
    }

    @Override
    public void create(String name, String address) {
        Pyme pyme = new Pyme();
        pyme.setAddress(address);
        pyme.setName(name);
        create(pyme);

    }

    @Override
    public int getIdByName(String name) {
        TypedQuery<Pyme> query = em.createNamedQuery("Pyme.findByName", Pyme.class).setParameter("name", name);
        List<Pyme> pymes = query.getResultList();
        return pymes.get(0).getIdentification();
    }

    @Override
    public String getNamePyme(int pymeIdentification) {
        return this.find(pymeIdentification).getName();
    }

    @Override
    public Integer getIdPyme(String namePyme) {
        Integer id = null;
        for (Pyme p : this.findAll()) {
            if (p.getName().equals(namePyme)) {
                id = p.getIdentification();
            }
        }
        return id;
    }
}
